#ifndef THINK_THINK_MODEL_RELATION_ONETOONE_H
#define THINK_THINK_MODEL_RELATION_ONETOONE_H

static char* think_model_relation_onetoone =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\model\\relation;\n"
	"\n"
	"use think\\db\\Query;\n"
	"use think\\Exception;\n"
	"use think\\Loader;\n"
	"use think\\Model;\n"
	"use think\\model\\Relation;\n"
	"\n"
	"/**\n"
	" * Class OneToOne\n"
	" * @package think\\model\\relation\n"
	" *\n"
	" */\n"
	"abstract class OneToOne extends Relation\n"
	"{\n"
	"    // 预载入方式 0 -JOIN 1 -IN\n"
	"    protected $eagerlyType = 1;\n"
	"    // 当前关联的JOIN类型\n"
	"    protected $joinType;\n"
	"    // 要绑定的属性\n"
	"    protected $bindAttr = [];\n"
	"    // 关联方法名\n"
	"    protected $relation;\n"
	"\n"
	"    /**\n"
	"     * 设置join类型\n"
	"     * @access public\n"
	"     * @param string $type JOIN类型\n"
	"     * @return $this\n"
	"     */\n"
	"    public function joinType($type)\n"
	"    {\n"
	"        $this->joinType = $type;\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询（JOIN方式）\n"
	"     * @access public\n"
	"     * @param Query    $query       查询对象\n"
	"     * @param string   $relation    关联名\n"
	"     * @param string   $subRelation 子关联\n"
	"     * @param \\Closure $closure     闭包条件\n"
	"     * @param bool     $first\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerly(Query $query, $relation, $subRelation, $closure, $first)\n"
	"    {\n"
	"        $name = Loader::parseName(basename(str_replace(\'\\\\\', \'/\', get_class($query->getModel()))));\n"
	"\n"
	"        if ($first) {\n"
	"            $table = $query->getTable();\n"
	"            $query->table([$table => $name]);\n"
	"            if ($query->getOptions(\'field\')) {\n"
	"                $field = $query->getOptions(\'field\');\n"
	"                $query->removeOption(\'field\');\n"
	"            } else {\n"
	"                $field = true;\n"
	"            }\n"
	"            $query->field($field, false, $table, $name);\n"
	"            $field = null;\n"
	"        }\n"
	"\n"
	"        // 预载入封装\n"
	"        $joinTable = $this->query->getTable();\n"
	"        $joinAlias = $relation;\n"
	"        $query->via($joinAlias);\n"
	"\n"
	"        if ($this instanceof BelongsTo) {\n"
	"            $query->join([$joinTable => $joinAlias], $name . \'.\' . $this->foreignKey . \'=\' . $joinAlias . \'.\' . $this->localKey, $this->joinType);\n"
	"        } else {\n"
	"            $query->join([$joinTable => $joinAlias], $name . \'.\' . $this->localKey . \'=\' . $joinAlias . \'.\' . $this->foreignKey, $this->joinType);\n"
	"        }\n"
	"\n"
	"        if ($closure) {\n"
	"            // 执行闭包查询\n"
	"            call_user_func_array($closure, [ & $query]);\n"
	"            // 使用withField指定获取关联的字段，如\n"
	"            // $query->where([\'id\'=>1])->withField(\'id,name\');\n"
	"            if ($query->getOptions(\'with_field\')) {\n"
	"                $field = $query->getOptions(\'with_field\');\n"
	"                $query->removeOption(\'with_field\');\n"
	"            }\n"
	"        } elseif (isset($this->option[\'field\'])) {\n"
	"            $field = $this->option[\'field\'];\n"
	"        }\n"
	"        $query->field(isset($field) ? $field : true, false, $joinTable, $joinAlias, $relation . \'__\');\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     *  预载入关联查询（数据集）\n"
	"     * @param array    $resultSet\n"
	"     * @param string   $relation\n"
	"     * @param string   $subRelation\n"
	"     * @param \\Closure $closure\n"
	"     * @return mixed\n"
	"     */\n"
	"    abstract protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure);\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询（数据）\n"
	"     * @param Model    $result\n"
	"     * @param string   $relation\n"
	"     * @param string   $subRelation\n"
	"     * @param \\Closure $closure\n"
	"     * @return mixed\n"
	"     */\n"
	"    abstract protected function eagerlyOne(&$result, $relation, $subRelation, $closure);\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询（数据集）\n"
	"     * @access public\n"
	"     * @param array    $resultSet   数据集\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        if (1 == $this->eagerlyType) {\n"
	"            // IN查询\n"
	"            $this->eagerlySet($resultSet, $relation, $subRelation, $closure);\n"
	"        } else {\n"
	"            // 模型关联组装\n"
	"            foreach ($resultSet as $result) {\n"
	"                $this->match($this->model, $relation, $result);\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询（数据）\n"
	"     * @access public\n"
	"     * @param Model    $result      数据对象\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResult(&$result, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        if (1 == $this->eagerlyType) {\n"
	"            // IN查询\n"
	"            $this->eagerlyOne($result, $relation, $subRelation, $closure);\n"
	"        } else {\n"
	"            // 模型关联组装\n"
	"            $this->match($this->model, $relation, $result);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 保存（新增）当前关联数据对象\n"
	"     * @access public\n"
	"     * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键\n"
	"     * @return Model|false\n"
	"     */\n"
	"    public function save($data)\n"
	"    {\n"
	"        if ($data instanceof Model) {\n"
	"            $data = $data->getData();\n"
	"        }\n"
	"        $model = new $this->model;\n"
	"        // 保存关联表数据\n"
	"        $data[$this->foreignKey] = $this->parent->{$this->localKey};\n"
	"        return $model->save($data) ? $model : false;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置预载入方式\n"
	"     * @access public\n"
	"     * @param integer $type 预载入方式 0 JOIN查询 1 IN查询\n"
	"     * @return $this\n"
	"     */\n"
	"    public function setEagerlyType($type)\n"
	"    {\n"
	"        $this->eagerlyType = $type;\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取预载入方式\n"
	"     * @access public\n"
	"     * @return integer\n"
	"     */\n"
	"    public function getEagerlyType()\n"
	"    {\n"
	"        return $this->eagerlyType;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 绑定关联表的属性到父模型属性\n"
	"     * @access public\n"
	"     * @param mixed $attr 要绑定的属性列表\n"
	"     * @return $this\n"
	"     */\n"
	"    public function bind($attr)\n"
	"    {\n"
	"        if (is_string($attr)) {\n"
	"            $attr = explode(\',\', $attr);\n"
	"        }\n"
	"        $this->bindAttr = $attr;\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取绑定属性\n"
	"     * @access public\n"
	"     * @return array\n"
	"     */\n"
	"    public function getBindAttr()\n"
	"    {\n"
	"        return $this->bindAttr;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 关联统计\n"
	"     * @access public\n"
	"     * @param Model    $result  数据对象\n"
	"     * @param \\Closure $closure 闭包\n"
	"     * @return integer\n"
	"     */\n"
	"    public function relationCount($result, $closure)\n"
	"    {\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 一对一 关联模型预查询拼装\n"
	"     * @access public\n"
	"     * @param string $model    模型名称\n"
	"     * @param string $relation 关联名\n"
	"     * @param Model  $result   模型对象实例\n"
	"     * @return void\n"
	"     */\n"
	"    protected function match($model, $relation, &$result)\n"
	"    {\n"
	"        // 重新组装模型数据\n"
	"        foreach ($result->getData() as $key => $val) {\n"
	"            if (strpos($key, \'__\')) {\n"
	"                list($name, $attr) = explode(\'__\', $key, 2);\n"
	"                if ($name == $relation) {\n"
	"                    $list[$name][$attr] = $val;\n"
	"                    unset($result->$key);\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"\n"
	"        if (isset($list[$relation])) {\n"
	"            $relationModel = new $model($list[$relation]);\n"
	"            $relationModel->setParent(clone $result);\n"
	"            $relationModel->isUpdate(true);\n"
	"\n"
	"            if (!empty($this->bindAttr)) {\n"
	"                $this->bindAttr($relationModel, $result, $this->bindAttr);\n"
	"            }\n"
	"        } else {\n"
	"            $relationModel = null;\n"
	"        }\n"
	"        $result->setRelation(Loader::parseName($relation), $relationModel);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 绑定关联属性到父模型\n"
	"     * @access protected\n"
	"     * @param Model $model    关联模型对象\n"
	"     * @param Model $result   父模型对象\n"
	"     * @param array $bindAttr 绑定属性\n"
	"     * @return void\n"
	"     * @throws Exception\n"
	"     */\n"
	"    protected function bindAttr($model, &$result, $bindAttr)\n"
	"    {\n"
	"        foreach ($bindAttr as $key => $attr) {\n"
	"            $key = is_numeric($key) ? $attr : $key;\n"
	"            if (isset($result->$key)) {\n"
	"                throw new Exception(\'bind attr has exists:\' . $key);\n"
	"            } else {\n"
	"                $result->setAttr($key, $model ? $model->$attr : null);\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 一对一 关联模型预查询（IN方式）\n"
	"     * @access public\n"
	"     * @param object        $model       关联模型对象\n"
	"     * @param array         $where       关联预查询条件\n"
	"     * @param string        $key         关联键名\n"
	"     * @param string        $relation    关联名\n"
	"     * @param string        $subRelation 子关联\n"
	"     * @param bool|\\Closure $closure\n"
	"     * @return array\n"
	"     */\n"
	"    protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = \'\', $closure = false)\n"
	"    {\n"
	"        $this->baseQuery = true;\n"
	"\n"
	"        // 预载入关联查询 支持嵌套预载入\n"
	"        if ($closure) {\n"
	"            call_user_func_array($closure, [ & $model]);\n"
	"            if ($field = $model->getOptions(\'with_field\')) {\n"
	"                $model->field($field)->removeOption(\'with_field\');\n"
	"            }\n"
	"        }\n"
	"        $list = $model->where($where)->with($subRelation)->select();\n"
	"\n"
	"        // 组装模型数据\n"
	"        $data = [];\n"
	"        foreach ($list as $set) {\n"
	"            $data[$set->$key] = $set;\n"
	"        }\n"
	"        return $data;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建关联统计子查询\n"
	"     * @access public\n"
	"     * @param \\Closure $closure 闭包\n"
	"     * @param string   $name    统计数据别名\n"
	"     * @return string\n"
	"     */\n"
	"    public function getRelationCountQuery($closure, &$name = null)\n"
	"    {\n"
	"        throw new Exception(\'relation not support: withCount\');\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif